import * as api from '@/api/countdown'
import { Message } from 'element-ui'
export default{
  bind(el, binding, vnode) {
    const countdownOptions = Object.assign({}, binding.value)
    const timer = el.timer = countdownOptions.time || 60
    const dom = el.querySelector('.el-button>span')
    el.Interval = null
    el.begin = false
    el.addEventListener('click', e => {
      console.log(e)
      const mobile = el.dataset.mobile
      const templateId = el.dataset.template
      if (mobile) {
        if (!el.begin) {
          el.begin = true
          api.testSend({ mobile: mobile, template: templateId }).then(() => {
            Message.success('短信已发送，请注意查收')
          })
          clearInterval(el.Interval)
          el.Interval = setInterval(() => {
            if (el.timer > 0) {
              el.timer -= 1
              dom.innerHTML = `${el.timer}${countdownOptions.send}`
            } else {
              el.timer = timer
              dom.innerHTML = `${countdownOptions.text}`
              el.begin = false
              clearInterval(el.Interval)
            }
          }, 1000)
        }
      } else {
        Message.error(countdownOptions.error || '请填写用户名!~')
      }
    }, false)
  }
}

